<style scoped lang="less">
.layout {
  border: 1px solid #d7dde4;
  background: #f5f7f9;
  position: relative;
  border-radius: 4px;
  overflow: hidden;
  height: 100%;
  .ivu-layout {
    height: 100%;
    &-content {
      height: 100%;
    }
  }
}
.layout-header-bar {
  background: #fff;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
.layout-logo-left {
  width: 90%;
  height: 30px;
  background: #5b6270;
  border-radius: 3px;
  margin: 15px auto;
}
.menu-icon {
  transition: all 0.3s;
  cursor: pointer;
}
.rotate-icon {
  transform: rotate(-90deg);
}
.menu-item span {
  display: inline-block;
  overflow: hidden;
  width: 69px;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: bottom;
  transition: width 0.2s ease 0.2s;
}
.menu-item i {
  transform: translateX(0px);
  transition: font-size 0.2s ease, transform 0.2s ease;
  vertical-align: middle;
  font-size: 16px;
}
.collapsed-menu span {
  width: 0px;
  transition: width 0.2s ease;
}
.collapsed-menu i {
  transform: translateX(5px);
  transition: font-size 0.2s ease 0.2s, transform 0.2s ease 0.2s;
  vertical-align: middle;
  font-size: 22px;
}
</style>
<template>
  <div class="layout">
    <Layout>
      <Sider ref="side1"
             hide-trigger
             collapsible
             :collapsed-width="78"
             v-model="isCollapsed">
        <Menu theme="dark"
              :active-name='activeName'
              width="auto"
              :class="menuitemClasses"
              @on-select="changeMenu">
          <MenuItem v-for="(item,index) of navLists"
                    :key='index'
                    :name="item.name">
          <Icon :type="item.icon" />
          <span>{{item.label}}</span>
          </MenuItem>
        </Menu>
      </Sider>
      <Layout>
        <Header :style="{padding: 0}"
                class="layout-header-bar">
          <Icon @click.native="collapsedSider"
                :class="rotateIcon"
                :style="{margin: '0 20px'}"
                type="md-menu"
                size="24"></Icon>
          <Dropdown style="float:right;margin-right:50px;"
                    @on-click="handle">
            <a href="javascript:void(0)">{{ userInfo ? userInfo.staff.username : '--' }}</a>
            <DropdownMenu slot="list">
              <DropdownItem name='out'>退出登录</DropdownItem>
            </DropdownMenu>
          </Dropdown>
        </Header>
        <Content :style="{margin: '20px', background: '#fff', minHeight: '260px',padding:'20px'}">
          <router-view></router-view>
        </Content>
      </Layout>
    </Layout>
  </div>
</template>
<script>
import server from '@/api/user';
import { mapState, mapMutations } from 'vuex';

export default {
  data () {
    return {
      isCollapsed: true,
      navLists: [
        { name: 'Home', label: '首页', icon: 'ios-home' },
        { name: 'NewsList', label: '新闻列表', icon: 'ios-list-box' },
        { name: 'NewsPbulish', label: '新闻发布', icon: 'ios-add-circle' },
      ],
    };
  },
  computed: {
    ...mapState(['userInfo']),
    rotateIcon () {
      return ["menu-icon", this.isCollapsed ? "rotate-icon" : ""];
    },
    menuitemClasses () {
      return ["menu-item", this.isCollapsed ? "collapsed-menu" : ""];
    },
    activeName () {
      for (const item of this.navLists) {
        if (this.$route.name.indexOf(item.name) !== -1) {
          return item.name;
        }
      }
      return '';
    },
  },
  methods: {
    ...mapMutations(['setUserInfo']),
    collapsedSider () {
      this.$refs.side1.toggleCollapse();
    },
    changeMenu (active) {
      this.$router.push({ name: active });
    },

    async handle (action) {
      if (action === 'out') {
        // 退出登录
        const res = await server.signout();
        if (!res.code) {
          this.$router.push({ name: 'Login' });
        }
      }
    },
  },
  async created () {
    // 获取用户信息
    const res = await server.getUserInfo();
    if (!res.code) {
      this.setUserInfo(res.data);
    }
  },
};
</script>
